package cn.fl.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCommRecService;
import cn.fl.customer.transform.CstCommRecVOTransform;
import cn.fl.customer.vo.CstCommRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.dto.CstCommRecDTO;

@Service
@SuppressWarnings("unchecked")
public class CstCommRecServiceImpl implements ICstCommRecService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CstCommRecServiceImpl.class);
	
	/**
	 * 客户模块 API服务
	 */
	@Autowired
	private ICustomerApiService customerApiService;
	
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CstCommRecVO selectById(Long id) {
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "查询ID参数不存在!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		CstCommRecDTO dto = this.customerApiService.selectCstCommRecById(id);
		if(null != dto){
			this.setWithDicInfo(dto, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		}
		return CstCommRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(CstCommRecVO entity) {
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象参数不存在!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.customerApiService.addCstCommRec(CstCommRecVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) {
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键ID参数不存在!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		List<Long> ids = new ArrayList<>();
		ids.add(id);
		return this.customerApiService.removeCstCommRecByIds(ids);
	}

	@Override
	public boolean modify(CstCommRecVO entity) {
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.customerApiService.modifyCstCommRec(CstCommRecVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) {
		if(null == page)page = new Pager();
		page = this.customerApiService.searchCstCommRecPage(page);
		List<CstCommRecDTO> list = (List<CstCommRecDTO>)page.getResultList();
		//判断数据是不是自己的,如果不是自己的只能查看,不能修改和删除
		Long usrId = CurrentThreadContext.getCurrentUserId();
		if(CollectionUtils.isNotEmpty(list)){
			for (CstCommRecDTO cstCommRecDTO : list) {
				if(null != cstCommRecDTO.getCrtUsrId() && usrId.equals(cstCommRecDTO.getCrtUsrId())){
					cstCommRecDTO.setIsOper(BaseConstant.IS_YESNO_YES);
				}else{
					cstCommRecDTO.setIsOper(BaseConstant.IS_YESNO_NO);
				}
			}
			page.setResultList(CstCommRecVOTransform.toVOList(list));
		}
		return page;
	}

	@Override
	public List<KeyValueVO> searchCommWayCdList() {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_COMM_WAY_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}
	
	/**
	 * 设置数据字典
	 * @param list
	 */
	private void setWithDicInfo(CstCommRecDTO cstCommRecDTO, String sysCd, String orgCd){
		if(null == cstCommRecDTO)return;
		try {
				SysDictionaryDTO commWayCdObj = this.sysDictionaryApiService.getCode(sysCd, orgCd, DictionaryConstant.CODE_GRP_COMM_WAY_CD, cstCommRecDTO.getCommWayCd());
				if(null != commWayCdObj){
					cstCommRecDTO.setCommWayCdNm(commWayCdObj.getCodeNm());
				}
		} catch (BaseException e) {
				BaseLogUtils.info(logger, "setWithDicInfo", "设置集合数据字典项[commWayCd:"+cstCommRecDTO.getCommWayCd()+"]不存在!", CurrentThreadContext.getCurrentUserName());
		}
	}
}
